package com.rslai.commons.validator.jsonvalidator.matchers;

import org.hamcrest.Description;
import org.hamcrest.Factory;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;

import java.util.ArrayList;
import java.util.List;

/**
 * Allowed
 */
public class Allowed extends TypeSafeMatcher<Object> {
    List<Object> list;

    /**
     * 构造参数
     * @param list List
     */
    public Allowed(List<Object> list) {
        this.list = list;
    }

    /**
     * 安全匹配，将永远不会为空
     * @param item 匹配内容
     * @return boolean
     */
    @Override
    protected boolean matchesSafely(Object item) {
        return getList().contains(item);
    }

    /**
     * 产生描述
     * @param description 描述
     */
    @Override
    public void describeTo(Description description) {
        description.appendText("allowed ").appendValue(list);
    }

    @Factory
    public static Matcher<Object> allowed(List<Object> maxNumber) {
        return new Allowed(maxNumber);
    }

    /**
     * 返回list
     * @return List
     */
    public List<Object> getList() {
        return list == null ? new ArrayList<Object>(0) : list;
    }

}
